class LibService {
    uuid4() {
        return "10000000-1000-4000-8000-100000000000".replace(/[018]/g, c =>
            (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
        )
    }

    sleep(milliseconds) {
        return new Promise((resolve) => setTimeout(resolve, milliseconds));
    }

    stringEndsWith(str, suffix) {
        return str.indexOf(suffix, str.length - suffix.length) !== -1;
    }

    isImage(filename) {
        const ext = ['.jpg', '.jpeg', '.png', '.svg', '.gif', '.webp']
        let ok = false
        for (let i = 0; i < ext.length; i++) {
            if (this.stringEndsWith(filename, ext[i])) {
                ok = true
            }
        }
        return ok
    }

    goUrl = (url, target = '_self') => {
        const link = document.createElement('a')
        link.href = url
        link.target = target
        link.click()
    }
}

export default new LibService()
